package ui;

import service.UserService;
import factory.ServiceFactory;
import model.User;
import util.InputValidator;
import java.util.Scanner;

public class LoginUI { // 用户登录界面 完成人：姜凯严 7/3
    private UserService userService = ServiceFactory.getUserService();

    public User login() {
        Scanner scanner = new Scanner(System.in);
        
        // 使用InputValidator工具类进行用户名校验
        String username = InputValidator.getValidNonEmptyString(scanner, "请输入用户名: ", "用户名");
        if (username == null) return null; // 校验失败
        
        // 使用InputValidator工具类进行密码校验
        String password = InputValidator.getValidNonEmptyString(scanner, "请输入密码: ", "密码");
        if (password == null) return null; // 校验失败

        User user = userService.login(username, password);
        if (user != null) {
            System.out.println("登录成功，欢迎 " + user.getUsername() + "！（权限：" + user.getPermissionLevel().name() + "）");
            return user;
        } else {
            System.out.println("用户名或密码错误！");
            return null;
        }
    }
}
